﻿using MediatR;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageOrderTable.Queries
{
    public class GetOrderStateByDeViceIdQueries : IRequest<Json<int>>
    {
        /// <summary>
        /// 设备id
        /// </summary>
        public int deviceid { get; set; }
    }

    public class GetOrderStateByDeViceIdQueriesHandler : IRequestHandler<GetOrderStateByDeViceIdQueries, Json<int>>
    {
        private readonly IordertableRepository _iordertableRepository;
        private readonly ICurrentUserService _icurrentUserServices;
        public GetOrderStateByDeViceIdQueriesHandler(IordertableRepository iordertableRepository, ICurrentUserService icurrentUserServices)
        {
            _iordertableRepository = iordertableRepository;
            _icurrentUserServices = icurrentUserServices;
        }
        public async Task<Json<int>> Handle(GetOrderStateByDeViceIdQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServices.GetUserId();
            if (userid <= 0)
            {
                return new Json<int>() { Status = 1003, Detail = "请先登录", Data = 0 };
            }
            var model = await _iordertableRepository.getOrderTalbeStateByDeviceid(request.deviceid,userid, cancellationToken);
            if (model != null && model.id > 0)
            {
                if (model.state == 1)
                {
                    return new Json<int>() { Status = 1000, Detail = "操作完成", Data = model.state };
                }
                else if (model.state == 0 && model.operationstate == 3) //等待出酒
                {
                    return new Json<int>() { Status = 1000, Detail = "操作完成", Data = 3 };
                }
                else
                {
                    return new Json<int>() { Status = 1000, Detail = "操作完成", Data = 0 };
                }
            }
            else {
                return new Json<int>() { Status = 1003, Detail = "未找到当前订单", Data = 0 };
            }
            
        }
    }
}
